#region imported Namespaces
using System;
using System.Data;

using DataAccess = PHSRAG.Utility.DataAccess;
using SalaryDistribConstants = PHSRAG.Insight21.Personnel.Constants.SalaryDistributionsConstants;
#endregion

namespace PHSRAG.Insight21.Personnel
{
	#region InternalSalaryDistribution
	/// <summary>
	/// The InternalSalaryDistribution class is the facade over 
	/// the details for a single internal salary distribution.
	/// </summary>
	public class InternalSalaryDistribution:SalaryDistribution
	{
		#region Public Properties
		/// <summary>
		/// Get/Set the fund number for the fund id on which the salary distribution is valid
		/// </summary>
		public string FundNumber
		{
			get
			{
				return (string)distribRow[SalaryDistribConstants.FundNumberColumnName];
			}
			set
			{
				distribRow[SalaryDistribConstants.FundNumberColumnName] = value;
			}
		}
		/// <summary>
		/// Get the fund title
		/// Return an empty string if there is no title for the fund
		/// </summary>
		public override string Title
		{
			get
			{
				object title = distribRow[SalaryDistribConstants.ProjectTitleColumnName];
				return (title==DBNull.Value)?string.Empty:(string)title;
			}
			set
			{
				distribRow[SalaryDistribConstants.ProjectTitleColumnName] = value;
			}
		}
		#endregion

		#region Constructor
		/// <summary>
		/// Intialize the base class with the dataRow
		/// </summary>
		/// <param name="dataRow">DataRow with Internal Salary distribution data</param>
		public InternalSalaryDistribution(DataRow dataRow):base(dataRow)
		{}
		#endregion

		#region Public overriden methods
		/// <summary>
		/// Add a new internal salary distribution to the database
		/// Validate the entered data
		/// Build the sqlParam array with the params specific to internal salary distribution
		/// Call the base(SalaryDistribution) AddDistribution method
		/// </summary>
		/// <param name="connectionString">Database connection string</param>
		/// <param name="domainUserID">Domain user id of the logged in user</param>
		public override void AddDistribution(string connectionString,int domainUserID)
		{
			DataTable distribTable = distribRow.Table;

			ValidationsHelper.ValidateEffectiveDate
				(distribTable.DataSet,Convert.ToDateTime(EffectiveDate));
			//Avoid doing overlap validation if there are no rows already present in the table
			if(distribTable.Rows.Count > 1)
			{
				ValidationsHelper.ValidateDistributionOverlapforAdd
					(distribTable,Convert.ToDateTime(EffectiveDate),FundNumber);
			}
			
			ValidationsHelper.ValidateFundNumber
				(Utility.DataAccess.ConnectionString,FundNumber);
			
			BuildSQLParamArray();
			
			base.AddDistribution(connectionString,domainUserID);
		}
		/// <summary>
		/// Edit an existing internal salary distribution data in the database
		/// Validate the entered data
		/// Build the sqlParam array with the params specific to internal salary distribution
		/// Call the base(SalaryDistribution) UpdateDistribution method
		/// </summary>
		public override void UpdateDistribution(string connectionString,int domainUserID)
		{			
			DataTable distribTable = distribRow.Table;
			
			ValidationsHelper.ValidateEffectiveDate
				(distribTable.DataSet,Convert.ToDateTime(EffectiveDate));
			ValidationsHelper.ValidateDistributionOverlapforUpdate
				(distribTable,Convert.ToDateTime(EffectiveDate),DistributionID,FundNumber);			
			ValidationsHelper.ValidateFundNumber
				(Utility.DataAccess.ConnectionString,FundNumber);

			BuildSQLParamArray();
			
			base.UpdateDistribution(connectionString,domainUserID);
		}
		#endregion

		#region Private method
		/// <summary>
		/// Build the sql param array with the params specific to Internal salary distribution
		/// </summary>
		private void BuildSQLParamArray()
		{
			sqlParams = Utility.DataAccess.BuildSqlParameterArray
							("@fundNumber",SqlDbType.VarChar,FundNumber
							,"@title",SqlDbType.VarChar,Title);
		}
		#endregion
	}
	#endregion
}